package com.t282.crm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.t282.crm.entity.CstCustomer;
import com.t282.crm.entity.CstLost;
import com.t282.crm.service.CstCustomerService;
import com.t282.crm.service.CstLostService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Api("客户流失管理")
@Controller
@RequestMapping("/api/lost")
public class LostController {

    @Autowired
    private CstLostService lostService;

    @Autowired
    private CstCustomerService customerService;

    @ApiOperation("显示客户流失列表")
    @RequestMapping("findLost")
    public Object findLost(@RequestParam(defaultValue = "1") Integer index,
                           @RequestParam(defaultValue = "5") Integer limit,
                           String lstCustName, String lstCustManagerName,
                           String status, HttpServletRequest request){
        Page<CstLost> page = new Page<>(index,limit);
        QueryWrapper<CstLost> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(lstCustName)){
            wrapper.like("lst_cust_name",lstCustName);
        }
        if(!StringUtils.isEmpty(lstCustManagerName)){
            wrapper.like("lst_cust_manager_name",lstCustManagerName);
        }
        if(!StringUtils.isEmpty(status)){
            wrapper.eq("lst_status",status);
        }

        Page<CstLost> lostPage = lostService.page(page, wrapper);
        request.setAttribute("lostPage",lostPage);
        request.setAttribute("lstCustName",lstCustName);
        request.setAttribute("lstCustManagerName",lstCustManagerName);
        request.setAttribute("status",status);
        return "customerloss/list";
    }

    @ResponseBody
    @ApiOperation(value = "客户流失预警")
    @GetMapping("earlyOrders")
    public Object earlyOrders(String odrCustomer,Date date){
        QueryWrapper<CstCustomer> wrapper = new QueryWrapper<>();
        wrapper.eq("cust_name",odrCustomer);
        CstCustomer customer = customerService.getOne(wrapper);
        CstLost lost = new CstLost();
        lost.setLstCustNo(customer.getCustNo());
        lost.setLstCustName(customer.getCustName());
        lost.setLstLastOrderDate(date);
        lost.setLstCustManagerId(customer.getCustManagerId());
        lost.setLstCustManagerName(customer.getCustManagerName());
        boolean flag = lostService.save(lost);

        return flag;
    }

    @ApiOperation(value = "跳转暂缓流失页面")
    @GetMapping("todelay")
    public String todelay(Integer lstId, HttpServletRequest request){
        CstLost lost = lostService.getById(lstId);
        request.setAttribute("lost",lost);
        return "customerloss/delay";
    }

    @ApiOperation("暂缓流失")
    @PostMapping("delayLst")
    public String delayLst(Long lstId,String lstDelay,String lstDelays){
        CstLost cstLost = new CstLost();
        cstLost.setLstId(lstId);
        cstLost.setLstStatus("暂缓流失");
        if(lstDelay != null){
            cstLost.setLstDelay(lstDelay+lstDelays);
        }else{
            cstLost.getLstDelay();
        }
        boolean update = lostService.updateById(cstLost);
        if(update){
            return "redirect:/api/lost/findLost";
        }
        return "redirect:/api/lost/todelay?lstId="+lstId;
    }

    @ApiOperation(value = "跳转确认流失页面")
    @GetMapping("todelayok")
    public String todelayok(Integer lstId, HttpServletRequest request){
        CstLost lost = lostService.getById(lstId);
        request.setAttribute("lost",lost);
        return "customerloss/delayok";
    }

    @ApiOperation("确认流失")
    @PostMapping("delayLstOk")
    public String lostLstOk(Integer lstId,String reason){
        CstLost lost = lostService.getById(lstId);
        lost.setLstLostDate(new Date());
        lost.setLstStatus("确认流失");
        lost.setLstReason(reason);
        QueryWrapper<CstLost> wrapper = new QueryWrapper<>();
        wrapper.eq("lst_id",lstId);
        boolean flag = lostService.update(lost, wrapper);
        if(flag){
            return "redirect:/api/lost/findLost";
        }
        return "redirect:/api/lost/todelayok?lstId="+lstId;
    }
}
